﻿using NPOI.HSSF.UserModel;
using OMG.Fill.Vegetable.Model;
using OMG.Fill.Vegetable.Service;
using OMG.Fill.Vegetables.Model.Ent;
using OMG.Fill.Vegetables.Service;
using ProjectShare;
using System;
using System.Collections.Generic;
using System.IO;
using System.Text;
using System.Web;
using System.Web.Http;
namespace OMG.Fill.Vegetables.Model.UI.Controllers
{
    public class VegColdStorageController : ApiController
    {
        VegColdStorageServices vegColdStorageServices = new VegColdStorageServices();


        //[ApiActionAuthorize("获取蔬菜冷库调查表T_VegColdStorage分页列表对象操作")]
        //[ApiActionException]
        public Pager<List<VegColdStorageEntity>> PostPageList(VegColdStorageQuery query)
        {
            return vegColdStorageServices.GetPageList(query);
        }
        //[ApiActionAuthorize("新增蔬菜冷库调查表T_VegColdStorage指定对象操作")]
        //[ApiActionException]
        public ResResult<int> Post(List<VegColdStorageEntity> model) {
            return vegColdStorageServices.AddInfo(model);
        }
        //[ApiActionAuthorize("修改蔬菜冷库调查表T_VegColdStorage指定对象操作")]
        //[ApiActionException]
        public ResResult<int> Put(VegColdStorageEntity model)
        {
            return vegColdStorageServices.UpdateInfo(model);
        }
        //[ApiActionAuthorize("删除蔬菜冷库调查表T_VegColdStorage指定对象操作")]
        //[ApiActionException]
        public ResResult<int> Delete(string id)
        {
            return vegColdStorageServices.DeleteInfo(id);
        }
        //[ApiActionAuthorize("获取蔬菜冷库调查表T_VegColdStorage指定对象操作")]
        //[ApiActionException]
        public ResResult<VegColdStorageEntity> Get(string id)
        {
            return vegColdStorageServices.GetModel(id);
        }
        /// <summary>
        /// 导出下载响应
        /// </summary>
        /// <param name="ReportCode"></param>
        public void GetExcelData(string ReportCode, string FillInYear, string AreaCode)
        {
            AreaServices areaServices = new AreaServices();
            string AreaName = areaServices.GetByCodeModel(AreaCode).model.Name;
            VegNowModel model = new VegNowModel();
            model.AreaCode = AreaCode;
            model.FillInYear = FillInYear;
            model.ReportCode = ReportCode;
            //打开完成excel文件并下载
            HttpContext curContext = HttpContext.Current;
            // 设置编码和附件格式
            curContext.Response.AddHeader("Access-Control-Allow-Origin", "*");
            curContext.Response.ContentType = "application/vnd.ms-excel";
            curContext.Response.ContentEncoding = Encoding.UTF8;
            curContext.Response.Charset = "";
            string strFileName = model.FillInYear + AreaName + "蔬菜冷库调查表.xls";
            curContext.Response.AppendHeader("Content-Disposition",
                "attachment;filename=" + HttpUtility.UrlEncode(strFileName, Encoding.UTF8));
            curContext.Response.BinaryWrite(vegColdStorageServices.Method(model).GetBuffer());
            curContext.Response.End();
        }
        public ResResult<List<VegColdStorageStatsModel>> PostStats(VegNowModel model)
        {
            return vegColdStorageServices.GetStatsList(model);
        }
        /// <summary>
        /// 导出下载响应
        /// </summary>
        /// <param name="ReportCode"></param>
        public void GetStatsExcelData(string AreaCode, string FillInYear,string Status)
        {
            AreaServices areaServices = new AreaServices();
            string AreaName = areaServices.GetByCodeModel(AreaCode).model.Name;
            VegNowModel model = new VegNowModel();
            model.AreaCode = AreaCode;
            model.FillInYear = FillInYear;
            model.Status = Status;
            //打开完成excel文件并下载
            HttpContext curContext = HttpContext.Current;
            // 设置编码和附件格式
            curContext.Response.AddHeader("Access-Control-Allow-Origin", "*");
            curContext.Response.ContentType = "application/vnd.ms-excel";
            curContext.Response.ContentEncoding = Encoding.UTF8;
            curContext.Response.Charset = "";
            string strFileName = model.FillInYear + AreaName + "蔬菜冷库统计表.xls";
            curContext.Response.AppendHeader("Content-Disposition",
                "attachment;filename=" + HttpUtility.UrlEncode(strFileName, Encoding.UTF8));
            curContext.Response.BinaryWrite(vegColdStorageServices.MethodStats(model).GetBuffer());
            curContext.Response.End();
        }
    }
}